package com.siki.config.filterConfig.loginStatus;

import com.alibaba.fastjson.JSON;
import com.siki.pojo.enumeration.config.GlobalServiceMsgCode;
import com.siki.utils.Deprecated.JsonResult;
import com.siki.utils.JsonInfoReturn;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
 * 权限拒绝访问逻辑处理器
 *
 * @Author Siki
 * @Date 2020/6/5
 */

@Component
public class CustomizeAccessDeniedHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException e) throws IOException, ServletException {
        httpServletResponse.setStatus(HttpServletResponse.SC_FORBIDDEN);
        JsonInfoReturn result = JsonInfoReturn.fail(GlobalServiceMsgCode.NO_PERMISSION);
        httpServletResponse.setContentType("text/json;charset=utf-8");
        httpServletResponse.getWriter().write(JSON.toJSONString(result));
    }
}
